#!/bin/bash
#
# Copyright(c) 2012-2021 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#

. $(dirname $3)/conf_framework

check() {
	cur_name=$(basename $2)
	config_file_path=$1
    if compile_module $cur_name "dev_t dev; lookup_bdev(\"some_path\", &dev)" "linux/blkdev.h"
    then
        echo $cur_name "1" >> $config_file_path
    elif compile_module $cur_name "lookup_bdev(\"some_path\")" "linux/fs.h" "linux/blkdev.h"
    then
        echo $cur_name "2" >> $config_file_path
    elif compile_module $cur_name "lookup_bdev(\"some_path\", 0)" "linux/fs.h" "linux/blkdev.h"
    then
        echo $cur_name "3" >> $config_file_path
    else
        echo $cur_name "X" >> $config_file_path
    fi
}

apply() {
    case "$1" in
    "1")
        add_function "
    static inline bool cas_bdev_exist(const char *path)
    {
        dev_t dev;
        int result;

        result = lookup_bdev(path, &dev);
        return !result;
    }"
        add_function "
    static inline bool cas_bdev_match(const char *path, struct block_device *bd)
    {
        dev_t dev;
        int result;

        result = lookup_bdev(path, &dev);
        if (result)
            return false;

        return (bd->bd_dev == dev);
    }" ;;
    "2")
        add_function "
    static inline bool cas_bdev_exist(const char *path)
    {
        struct block_device *bdev;

        bdev = lookup_bdev(path);
        if (IS_ERR(bdev))
            return false;
        bdput(bdev);
        return true;
    }"
        add_function "
    static inline bool cas_bdev_match(const char *path, struct block_device *bd)
    {
        struct block_device *bdev;
        bool match = false;

        bdev = lookup_bdev(path);
        if (IS_ERR(bdev))
            return false;
        match = (bdev == bd);
        bdput(bdev);
        return match;
    }" ;;
    "3")
        add_function "
    static inline bool cas_bdev_exist(const char *path)
    {
        struct block_device *bdev;

        bdev = lookup_bdev(path, 0);
        if (IS_ERR(bdev))
            return false;
        bdput(bdev);
        return true;
    }"
        add_function "
    static inline bool cas_bdev_match(const char *path, struct block_device *bd)
    {
        struct block_device *bdev;
        bool match = false;

        bdev = lookup_bdev(path, 0);
        if (IS_ERR(bdev))
            return false;
        match = (bdev == bd);
        bdput(bdev);
        return match;
    }" ;;
    *)
        exit 1
    esac
}

conf_run $@
